package com.crowdsourcing.controller;

import com.crowdsourcing.model.Msg;
import com.crowdsourcing.model.User;
import com.crowdsourcing.service.IUserServcie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author renbaojia 1406423298@qq.ocom
 * @version 1.0
 * @CREATED 2019/3/4 14:48:40
 * @des
 * @修改记录:
 **/
@Controller
@RequestMapping("/login")
public class LoginController {
    @Autowired
    IUserServcie iUserServcie;
    @Autowired
    HttpSession httpSession;

    /**
     * 登录
     *
     * @param user
     * @return
     */
    @ResponseBody
    @PostMapping("/userLogin")
    public Msg login(User user) {
        User users = iUserServcie.login(user);
        if (users != null) {
            httpSession.setAttribute("user", users);
            return Msg.success();
        }
        return Msg.error();
    }

    /**
     * 退出
     *
     * @return 返回首页
     */
    @GetMapping("/exit")
    public String exit() {
        httpSession.invalidate();
        return "index";
    }



}
